package com.zn.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zn.entity.AppCustomer;
import com.zn.service.AppCustomerService;
import com.zn.bestSignClient.BestSignService;
import com.zn.vo.Message;
import com.zn.web.dto.AppCustomerDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by admintrator on 2019/2/27.
 */
@RestController
@RequestMapping("/bestSign")
public class BestSignController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppAgreementController.class);
    @Autowired
    private BestSignService bestSignService;
    @Autowired
    private AppCustomerService appCustomerService;

    /**
     * 获取注册开户协议
     * @return
     */
    @RequestMapping(value = "/getTemplateVars",method = RequestMethod.POST)
    public ResponseEntity<Message> getTemplateVars(@RequestBody AppCustomerDto record) {
        try {
            Map<String, Object> map = new HashMap<>();
            LOGGER.info("getTemplateVars info............"+record.toString());
            AppCustomer appCustomer = appCustomerService.selectByPrimaryKey(record.getCustomerId());
            String str = bestSignService.getTemplateVars(appCustomer);
            JSONObject jsonObject = JSON.parseObject(str);
            for (String key : jsonObject.keySet()){
                map.put(key, jsonObject.get(key));
            }
            return ResponseEntity.ok(Message.successData(map));
        }catch (Exception ex){
            LOGGER.error("getTemplateVars............"+ex.getMessage());
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    /**
     * 用户注册
     * @param record
     * @return
     */
    @RequestMapping(value = "/regUser",method = RequestMethod.POST)
    public ResponseEntity<Message> regUser(@RequestBody AppCustomerDto record) {
        try {
            LOGGER.info("regUser info............"+record.toString());
            AppCustomer appCustomer = appCustomerService.selectByPrimaryKey(record.getCustomerId());
            if (appCustomer.getAuthenticationStatus() == 0 || StringUtils.isBlank(appCustomer.getIdCard())){
                return ResponseEntity.ok(Message.BESTSIGN_CUSTOMER_REAL_NAME);
            }
            int count = bestSignService.sendUserReg(appCustomer);
            if (count > 0) {
                return ResponseEntity.ok(Message.REQ_SUCCESS);
            }else {
                return ResponseEntity.ok(Message.SYS_ERROR);
            }
        }catch (Exception ex){
            LOGGER.error("regUser............"+ex.getMessage());
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    /**
     * 异步获取上上签通知信息接口
     * @param json
     * @return
     */
    @RequestMapping(value = "/ssqNoticeAction",method = RequestMethod.POST)
    public ResponseEntity<Message> ssqNoticeAction(@RequestBody String json) {
        try {
            LOGGER.info("ssqNoticeAction info--------->"+json);
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            int count = bestSignService.postUrlParams(json, request);
            if (count > 0) {
                return ResponseEntity.ok(Message.REQ_SUCCESS);
            }else if (count == -1){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }else {
                return ResponseEntity.ok(Message.SYS_ERROR);
            }
        }catch (Exception ex){
            LOGGER.error("ssqNoticeAction............"+ex.getMessage());
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    /**
     * 判断客户是否要签署上上签协议
     * @param record
     * @return
     */
    @RequestMapping(value = "/getAppCustomerBestSignStatus",method = RequestMethod.POST)
    public ResponseEntity<Message> getAppCustomerBestSignStatus(@RequestBody AppCustomerDto record) {
        try {
            LOGGER.info("getAppCustomerBestSignStatus info--------->"+record.toString());
            LOGGER.error(record.toString());
            AppCustomer appCustomer = new AppCustomer();
            appCustomer.setCustomerId(record.getCustomerId());
            Message message = bestSignService.checkCustomerSsqStatus(appCustomer);
            return ResponseEntity.ok(message);
        }catch (Exception ex){
            LOGGER.error("getAppCustomerBestSignStatus........."+record.toString());
            LOGGER.error(ex.getMessage());
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

}
